<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: send_order.php 304 2013-09-23 10:49:56Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 304 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-23 10:49:56 +0000 (Mon, 23 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require_once(DIR_FS_INC.'inc.get_attributes_model.php');
require_once(DIR_FS_INC.'inc.date_long.php');
require_once(DIR_WS_CLASSES.'fpdf/html_table.php');
require_once(DIR_FS_INC.'inc.build_order_id.php');

$order_check = $db->db_query("SELECT customers_id FROM ".TABLE_ORDERS." WHERE orders_id='".$insert_id."'");

if($_SESSION['customer_id'] == $order_check->fields['customers_id']) {

	$order = new order($insert_id);

	if ($_SESSION['paypal_express_new_customer'] == 'true' && $_SESSION['ACCOUNT_PASSWORD'] == 'true') {

		require_once (DIR_FS_INC.'inc.create_password.php');
		require_once (DIR_FS_INC.'inc.encrypt_password.php');

		$password_encrypted =  RandomString(10);
		$password = encrypt_password($password_encrypted);

		$db->db_query("UPDATE ".TABLE_CUSTOMERS." SET customers_password = '".$password."' WHERE customers_id = '".(int) $_SESSION['customer_id']."'");

		$smarty->assign('NEW_PASSWORD', $password_encrypted);
	}
	$smarty->assign('address_label_customer', address_format($order->customer['format_id'], $order->customer, 1, '', '<br />'));
	$smarty->assign('address_label_shipping', address_format($order->delivery['format_id'], $order->delivery, 1, '', '<br />'));
	if ($_SESSION['credit_covers'] != '1')
		$smarty->assign('address_label_payment', address_format($order->billing['format_id'], $order->billing, 1, '', '<br />'));

	$smarty->assign('csID', $order->customer['csID']);

	$smarty->assign('oID', build_order_id($order->info['date_purchased'], $insert_id, 'confirm'));
	if ($order->info['payment_method'] != '' && $order->info['payment_method'] != 'no_payment') {
		include (DIR_WS_LANGUAGES.$_SESSION['language'].'/modules/payment/'.$order->info['payment_method'].'.php');
		$payment_method = constant(strtoupper('MODULE_PAYMENT_'.$order->info['payment_method'].'_TEXT_TITLE'));
	}

	$customers_gender = $db->db_query("SELECT customers_gender FROM ".TABLE_CUSTOMERS." WHERE customers_id = '".$_SESSION['customer_id']."' ");
	$widerruf = $db->db_query("SELECT content_heading, content_text, content_file FROM ".TABLE_CONTENT_MANAGER." WHERE content_group = '10' AND languages_id = '".(int)$_SESSION['languages_id']."' ");

	$smarty->assign('GENDER', $customers_gender->fields['customers_gender']);
	$smarty->assign('VNAME', $order->customer['firstname']);
	$smarty->assign('NNAME', $order->customer['lastname']);
	$smarty->assign('PAYMENT_METHOD', $payment_method);
	$smarty->assign('DATE', date_long($order->info['date_purchased']));

	$order_data = $order->getOrderData($insert_id);
	$smarty->assign('order_data', $order_data);
  $smarty->assign('order_shipping_time', $order->shipping_time_name);
//print_r($order_data); die();
	$order_total = $order->getTotalData($insert_id);
	$smarty->assign('order_total', $order_total['data']);

	$smarty->assign('NAME', $order->customer['name']);
	$smarty->assign('COMMENTS', $order->info['comments']);
	$smarty->assign('EMAIL', $order->customer['email_address']);
	$smarty->assign('PHONE',$order->customer['telephone']);
	$smarty->assign('WIDERRUF_HEAD', $widerruf->fields['content_heading']);
	if($widerruf->fields['content_file'] !='') {
		ob_start();
		include (DIR_FS_CATALOG.'media/content/'.$widerruf->fields['content_file']);
		$text = utf8_encode(ob_get_contents());
		ob_end_clean();
		$smarty->assign('WIDERRUF_TEXT', $text);

	} else
		$smarty->assign('WIDERRUF_TEXT', $widerruf->fields['content_text']);

	// EU Bank Transfer
	if ($order->info['payment_method'] == 'eustandardtransfer') {
		$smarty->assign('PAYMENT_INFO_HTML', MODULE_PAYMENT_EUTRANSFER_TEXT_DESCRIPTION);
		$smarty->assign('PAYMENT_INFO_TXT', str_replace("<br />", "\n", MODULE_PAYMENT_EUTRANSFER_TEXT_DESCRIPTION));
	}

	// MONEYORDER
	if ($order->info['payment_method'] == 'moneyorder') {
		$smarty->assign('PAYMENT_INFO_HTML', MODULE_PAYMENT_MONEYORDER_TEXT_DESCRIPTION);
		$smarty->assign('PAYMENT_INFO_TXT', str_replace("<br />", "\n", MODULE_PAYMENT_MONEYORDER_TEXT_DESCRIPTION));
	}

	// CASH
	if ($order->info['payment_method'] == 'cash') {
		$smarty->assign('PAYMENT_INFO_HTML', MODULE_PAYMENT_CASH_TEXT_INFO);
		$smarty->assign('PAYMENT_INFO_TXT', str_replace("<br />", "\n", MODULE_PAYMENT_CASH_TEXT_INFO));
	}

	$smarty->caching = false;
	require(DIR_FS_INC.'inc.get_mail_body.php');
	$html_mail = $smarty->fetch('html:order_mail');
	$html_mail .= $signatur_html;
	$txt_mail = $smarty->fetch('txt:order_mail');
	$txt_mail .= $signatur_text;
	require(DIR_FS_INC.'inc.get_mail_data.php');
	$mail_data = get_mail_data('order_mail');

	// Betreff erzeugen
	$order_subject = str_replace('{$nr}', build_order_id($order->info['date_purchased'], $insert_id, 'confirm'), $mail_data['EMAIL_SUBJECT']);
	$order_subject = str_replace('{$date}', strftime(DATE_FORMAT_LONG), $order_subject);
	$order_subject = str_replace('{$lastname}', $order->customer['lastname'], $order_subject);
	$order_subject = str_replace('{$firstname}', $order->customer['firstname'], $order_subject);

  if (1==1 && defined('VIEWMAIL')) {
    if (VIEWMAIL == 'html') echo $html_mail;
    else if (VIEWMAIL == 'txt')  echo '<pre>'.$txt_mail.'</pre>';
    die();
  }
  //$order->customer['email_address'] = "d.mueller@itdotmedia.de";  

	// Mail an den Admin
	php_mail($order->customer['email_address'],
				$order->customer['firstname'].' '.$order->customer['lastname'],
				$mail_data['EMAIL_ADDRESS'],
				$mail_data['EMAIL_ADDRESS_NAME'],
				array($mail_data['EMAIL_FORWARD'], 'd.mueller@itdotmedia.de'), /* produktion@smr-rollo24.de d.mueller@itdotmedia.de */
				$order->customer['email_address'],
				$order->customer['firstname'].' '.$order->customer['lastname'],
				'',
				'',
				$order_subject,
				$html_mail,
				$txt_mail);

	// Mail an den Kunden
	php_mail($mail_data['EMAIL_ADDRESS'],
				$from_name,
				$order->customer['email_address'],
				$order->customer['firstname'].' '.$order->customer['lastname'],
				'',
				$mail_data['EMAIL_REPLAY_ADDRESS'],
				$from_name,
				'',
				'',
				$order_subject,
				$html_mail,
				$txt_mail,
				true);

	if (AFTERBUY_ACTIVATED == 'true') {
		require(DIR_WS_CLASSES.'class.afterbuy.php');
		$aBUY = new afterbuy_functions($insert_id);
		if ($aBUY->order_send())
			$aBUY->process_order();
	}

} else {
	$smarty->assign('ERROR', 'Sie dürfen diese Bestellung nicht sehen!');
	$smarty->display(CURRENT_TEMPLATE.'/module/error_message.html');
}
?>